package org.sam.swing.table.verification;

import java.awt.Component;

import org.jdesktop.swingx.decorator.ComponentAdapter;
import org.jdesktop.swingx.decorator.HighlightPredicate;
import org.sam.swing.table.JSTable;
import org.sam.swing.table.JSTableColumn;
import org.sam.swing.table.JSTableModel;

/**
 * 带校验功能的过滤器对象
 * 
 * @author sam
 *
 */
public class VerificationHighlightTablePredicate implements HighlightPredicate {

	/**
	 * 是否高亮显示 高亮显示和校验的逻辑是反的，true代表高亮，false代表校验通过
	 */
	@Override
	public boolean isHighlighted(Component renderer, ComponentAdapter adapter) {
		if (adapter.getComponent() instanceof JSTable) {
			JSTable table = (JSTable) adapter.getComponent();
			JSTableModel<?> model = (JSTableModel<?>) table.getModel();

			// 使用model校验对象
			if (model.getVerification() != null) {
				return !model.getVerification().check(model, adapter.row, adapter.column);
			}

			// 使用列校验
			if (model.isTableColumnVerificatior()) {
				if (adapter.column < 0 || adapter.column >= model.getColumnCount()){
					return false;
				}
				
				JSTableColumn col = model.getTableColumns()[adapter.column];
				if (col.getVerficator() != null){
					return !col.getVerficator().check(model, adapter.row, adapter.column);
				}
			}
			
			//使用客户端校验
			if (model.isUseClientVerification() && model.getTableModelLinster() != null){
				return !model.getTableModelLinster().check(model, null);
			}

		}
		return false;
	}
}
